/**
 *  Copyright Matt Silbernagel 2010
 *  
 *	This file is part of MobilePrinting.
 *
 *	MobilePrinting is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MobilePrinting is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MobilePrinting.  If not, see <http://www.gnu.org/licenses/>.
 */
package edu.uc.labs.MobilePrinting.OSUtils;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static edu.uc.labs.MobilePrinting.Configurator.*;

/**
 * This Class has utility methods that perform a multitude of sanity checks
 * @author Matt Silbernagel
 *
 */
public class SanityCheck {

	
	public static boolean isPrinterNameValid(String name){
		String invalidChars = "([^\\w])+";
		boolean isValid = false;		
		Pattern p = Pattern.compile(invalidChars);
		Matcher mat = p.matcher(name);
		if (mat.find()) {
			LOGGER.debug("The starting starting charactor that matched is "+ name.charAt(mat.start()));
			LOGGER.debug("The starting ending charactor that matched is "+ name.charAt(mat.end()));
			LOGGER.debug("The printer name is invalid...");
			isValid = false;
		} else { 
			isValid = true; 
		}
		return isValid;
	}
	
	public static String boolToString( boolean b ){
		String num = b ? "1" : "0";
		return num;
	}

	public static boolean stringToBool( String s ){
		if (s.equalsIgnoreCase("0")){
			return false;
		} else {
			return true;
		}
		
	}
	
	/**
	 * Method to display an Array in String format
	 * @param a the Array
	 * @param separator what array fields be seperated by
	 * 		  this will usually be a space (" ")
	 * @return
	 */
	public static String arrayToString(String[] a, String separator) {
		String result = "";
		if (a.length > 0) {
			result = a[0]; // start with the first element
			for (int i = 1; i < a.length; i++) {
				result = result + separator + a[i];
			}
		}
		return result;
	}

	
	public static String[] listToArray(ArrayList<String> cmd){
		// Convert the ArrayList to an Array
		int size = cmd.size();
		String[] command = new String[size];
		for(int i=0; i<size; i++){
			command[i] = cmd.get(i);
		}
		return command;
	}

}
